package com.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.beans.Movie;
import com.bussiness.MovieBussinessProxy;
import com.constants.ServiceEndPoints;
import com.constants.UIMessages;
import com.exceptions.MovieAlreadyExistsException;

/**
 * Servlet implementation class UpdateMovieServlet
 */
public class UpdateMovieServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	MovieBussinessProxy movieBI = new MovieBussinessProxy();  
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateMovieServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Movie addMovie = new Movie();
		Movie movie;
		String[] t;
		Movie[] allmovies;
		HttpSession session = request.getSession(true);
		try
		{
			
			RequestDispatcher dispatcher;
			movie=(Movie)session.getAttribute("editMovie");
			movieBI.setEndpoint(ServiceEndPoints.MovieEndPoint);
			int movieid = (Integer)session.getAttribute("editmovie");
			if(request.getParameter("movieName").toString().trim()=="" || request.getParameter("movieName").toString()== null
					|| request.getParameter("movieBanner").toString()== null || request.getParameter("movieBanner").toString().trim()=="")
			{
				request.setAttribute("ServerMessage", "Enter all required values" );
				dispatcher = getServletContext().getRequestDispatcher("/editmovie.jsp");
				dispatcher.forward(request, response);
				return;
			}
			
			addMovie.setMovieId(movieid);
			addMovie.setMovieName(request.getParameter("movieName").toString());
			addMovie.setMovieBanner(request.getParameter("movieBanner").toString());
			addMovie.setCategoryid(Integer.parseInt(request.getParameter("movieCat")));
			int total = Integer.parseInt(request.getParameter("copies"));
			addMovie.setTotalCopy(total);
			if(movie.getTotalCopy() < total){
				int i = movie.getTotalCopy() - total;
				int d =movie.getAvailableCopy()-i;
				addMovie.setAvailableCopy(d);
			}
			if(movie.getTotalCopy() > total){
				int i = total - movie.getTotalCopy();
				int d =movie.getAvailableCopy()+i;
				addMovie.setAvailableCopy(d);
			}
			if(movie.getTotalCopy() == total){
				int d =movie.getAvailableCopy();
				addMovie.setAvailableCopy(d);
			}
			
			addMovie.setRent(Double.parseDouble(request.getParameter("rent")));
			//t = request.getParameter("dateText").toString().split("-");
			addMovie.setReleaseDate(request.getParameter("dateText").toString());
			addMovie.setRating(0.0);
			addMovie.setNo_of_ratings(0);
			movieBI.updateMovie(addMovie);
			allmovies = movieBI.getAllMovies();
			
			session.setAttribute("allMovies",allmovies);
			request.setAttribute("ServerMessage","Edited Movie successfully");	 
			
			dispatcher = getServletContext().getRequestDispatcher("/listeditmovie.jsp");
			dispatcher.forward(request, response);
			
		}catch(MovieAlreadyExistsException e)
		{
			request.setAttribute("ServerMessage", "Movie with the same Name and Banner already exist" );
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/editmovie.jsp");
			dispatcher.forward(request, response);
		}
		catch(NumberFormatException f)
		{
			request.setAttribute("ServerMessage", "Enter correct input for numeric fields" );
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/editmovie.jsp");
			dispatcher.forward(request, response);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			request.setAttribute("ServerMessage", UIMessages.SERVICE_UNAVAILABLE);
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/editmovie.jsp");
			dispatcher.forward(request, response);
		}
	}

}
